package com.nitty.cash.loop.ui.adapter

import android.annotation.SuppressLint
import android.graphics.Color
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.lib.common.adapter.CommonRecyclerViewAdapter
import com.lib.common.adapter.CommonViewHolderHelper
import com.lib.common.shapeview.layout.ShapeLinearLayout
import com.nitty.cash.loop.R
import com.nitty.cash.loop.databinding.UiOrderRepayDetailsItemBinding
import org.json.JSONObject
import java.math.BigDecimal
import kotlin.math.abs

/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/11/19 10:43
 * @Annotation：订单列表适配器
 */
class OrderRepayAdapter(recyclerView: RecyclerView?) : CommonRecyclerViewAdapter<JSONObject>(recyclerView) {

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        return when (model.optBoolean("isEmptyView",false)){
            true -> {
                R.layout.ui_order_list_empty
            }
            else -> {
                val remainRepayDay = model.optInt("remainRepayDay")
                // remainRepayDay >= 7  -> normal
                // 0 < remainRepayDay < 7  -> days left
                // remainRepayDay < 0  -> days overdue
                if (remainRepayDay > 7){
                    R.layout.ui_order_repay_normal_item
                } else if(remainRepayDay in 0..7) {
                    R.layout.ui_order_repay_days_left_item
                } else {
                    R.layout.ui_order_repay_overdue_item
                }
            }
        }
    }

    override fun setItemChildListener(helper: CommonViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.textWithdraw)
        helper.setItemChildClickListener(R.id.btnOrderRepay)
        helper.setItemChildClickListener(R.id.imgOrderRepayOpen)
    }

    override fun fillData(helper: CommonViewHolderHelper, position: Int, model: JSONObject) {
        when (model.optBoolean("isEmptyView",false)){
            true -> {//空
                val textWithdraw = helper.getTextView(R.id.textWithdraw)
                textWithdraw.paint.flags = Paint.UNDERLINE_TEXT_FLAG//下划线
                textWithdraw.paint.isAntiAlias = true//抗锯齿
            }
            else -> {
                showNittyOrderRepayView(helper,position,model)
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun showNittyOrderRepayView(helper: CommonViewHolderHelper, position: Int, model: JSONObject){
        //单位
        val unit = model.optString("currency")
        val itemList = model.optJSONArray("list")
        val orderRepayTitleLayout = helper.getView<LinearLayout>(R.id.orderRepayTitleLayout)
        orderRepayTitleLayout.removeAllViews()
        val orderRepayItemLayout = helper.getView<ShapeLinearLayout>(R.id.orderRepayItemLayout)
        orderRepayItemLayout.removeAllViews()
        if (itemList != null){
            for (i in 0..<itemList.length()){
                val item = itemList.optJSONObject(i)
                val titleItemBinding = UiOrderRepayDetailsItemBinding.inflate(LayoutInflater.from(mContext))
                titleItemBinding.textShowTitle.text = item.optString("showName")
                var showDetail = item.optString("showDetail")
                //是否包含单位
                if (showDetail.contains(unit)){
                    showDetail = showDetail.replace(unit,"")
                    showDetail = "${BigDecimal(showDetail).stripTrailingZeros().toPlainString()} $unit"
                }
                titleItemBinding.textShowValue.text = showDetail
                if (item.optString("title") == "YES"){
                    titleItemBinding.textShowTitle.setTextColor(Color.parseColor("#FF887A61"))
                    orderRepayTitleLayout.addView(titleItemBinding.root)
                } else {
                    titleItemBinding.textShowTitle.setTextColor(Color.parseColor("#FF4A403D"))
                    orderRepayItemLayout.addView(titleItemBinding.root)
                }
            }
        }

        val remainRepayDay = model.optInt("remainRepayDay")
        // remainRepayDay >= 7  -> normal
        // 0 < remainRepayDay < 7  -> days left
        // remainRepayDay < 0  -> days overdue
        if (remainRepayDay > 7){
            helper.setText(R.id.textOrderStatus,mContext.getString(R.string.order_repay_normal))
        } else if(remainRepayDay in 0..7) {
            helper.setText(R.id.textOrderStatus,String.format(mContext.getString(R.string.order_repay_days_left),remainRepayDay))
        } else {
            helper.setText(R.id.textOrderStatus,String.format(mContext.getString(R.string.order_repay_days_overdue), abs(remainRepayDay)))
        }

        val imgOrderRepayOpen = helper.getImageView(R.id.imgOrderRepayOpen)
        when(model.optBoolean("isOpen",false)){
            true -> {
                imgOrderRepayOpen.setImageResource(R.mipmap.nitty_up_black)
                orderRepayItemLayout.visibility = View.VISIBLE
            }
            else -> {
                imgOrderRepayOpen.setImageResource(R.mipmap.nitty_down_black)
                orderRepayItemLayout.visibility = View.GONE
            }
        }
    }
}